package com.adingxiong.pm.controller;

import com.adingxiong.pm.entity.Department;
import com.adingxiong.pm.model.Result;
import com.adingxiong.pm.model.RetCode;
import com.adingxiong.pm.server.DepartServer;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *  api分类  接口基本描述说明
 */
@RestController
@RequestMapping("/depart")
@Api( tags = "角色管理")   //如果为定义tag 默认使用类名当做接口名称
public class DepartController {

    @Autowired
    private DepartServer departServer;

    @ApiOperation(value = "根据id查询角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id" ,value ="角色id" , paramType = "query" , required = true)
    })
    @RequestMapping(value = "/findone" ,method = RequestMethod.GET)
    public Department findOne(Integer id){
        return departServer.getById(id);
    }

    @ApiOperation(value ="查询所有的角色")
    @RequestMapping(value = "/findAll" ,method = RequestMethod.GET)
    public List<Department> findAll(){
        return departServer.list();
    }

    @ApiOperation(value ="新增角色")
    @RequestMapping(value = "/savaInfo" , method = RequestMethod.POST)
    public Result saveDepart(@RequestBody  Department department ){

        boolean insert =  departServer.save(department);
        if(insert){
            return new Result().setCode(RetCode.SUCCESS).setMsg("新增角色成功");
        }else{
            return new Result().setCode(RetCode.SUCCESS).setMsg("新增角色成功");
        }
    }

    /**
     * 分页查询
     */
    @ApiOperation(value ="分页查询")
    @RequestMapping(value = "/getInfoListPage" ,  method = RequestMethod.GET)
    public Result getRoleList (Integer page, Integer size){
        Page<Department> departList = departServer.page(new Page<>(page ,size));
        return new Result().setCode(RetCode.SUCCESS).setMsg("查询成功").setData(departList);
    }
    /*public IPage<Department> getInfoListPage(){
        IPage<Department> page = new Page<>();
        page.setCurrent(1); //设置当前页数
        page.setSize(1);  //设置每页条数
        page.setPages(2);
        page.setTotal(3);
        page = departServer.page(page);
        return  page;
    }*/

    /**
     * 插入多条数据
     */
    @ApiOperation(value ="插入多条数据")
    @RequestMapping(value = "/inserts" ,method = RequestMethod.GET)
    public void InertMany (){
        Department department = new Department();
        department.setDepartmentName("UI设计师");

        Department department1 = new Department();
        department1.setDepartmentName("web前端工程师");

        List<Department> list = new ArrayList<>();
        list.add(department);
        list.add(department1);

        departServer.saveBatch(list);
    }

    @ApiOperation(value = "根据id修改角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "department" ,value ="角色对象信息", paramType = "query")
    })
    @RequestMapping(value = "/updateRoleById" ,method = RequestMethod.POST)
    public Result updateRoleById(Department department){
        boolean update = departServer.updateById(department);
        if(update){
            return new Result().setCode(RetCode.SUCCESS).setMsg("修改角色成功");
        }else{
            return new Result().setCode(RetCode.SUCCESS).setMsg("修改角色成功");
        }
    }

}
